package org.framegen.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.framegen.core.FrameGenEntry;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;

public class GeneratorMain {
    public static void main(String[] args) {
        try {
            // 1. 加载MyBatis配置文件
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            // 2. 获取数据源实例
            DataSource dataSource = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource();

            new FrameGenEntry(dataSource)
                    .outModule("framegen-example-corejava-mybatis")
                    .setPackage(builder -> {
                        builder.origin("org.framegen.example")
                                .model("model")
                        ;
                    })
                    .includes("user")
                    .lombok()
                    .mybatisPlus()
                    .run(GeneratorMain.class);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}